package com.school.lab.dao.impl;

import java.util.List;

import org.hibernate.CacheMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.school.lab.common.dao.impl.GenericDaoHibernateImpl;
import com.school.lab.dao.AclDao;
import com.school.lab.domain.Acl;

public class AclDaoImpl extends GenericDaoHibernateImpl<Acl, Long> implements AclDao {

	
	@SuppressWarnings("unchecked")
	public List<Acl> findAclByRole(Long roleId) {
		String hql = "from Acl a where a.role.id =:roleId";
		Query query = getSession().createQuery(hql);		
		query.setLong("roleId", roleId);
		query.setCacheable(true);
		query.setCacheMode(CacheMode.GET);
		return query.list();
	}

	
	public Acl getAcl(Long roleId, Long moudelId) {
		StringBuilder hql = new StringBuilder();
		hql.append("select acl.id from T_Acl acl where acl.role_id =? and acl.moudel_id=?");
		Session session = super.getSession();
		Query query = session.createSQLQuery(hql.toString());
		query.setParameter(0, roleId);
		query.setParameter(1, moudelId);
		query.setCacheable(true);
		query.setCacheMode(CacheMode.GET);
		if(query.uniqueResult()!=null){		
		Long aclId= Long.parseLong(query.uniqueResult().toString());
		return this.get(aclId);
		}else {
			return null;
		}
		
	}

}
